<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Api\CommonController;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class PermissionsController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {
            $params = $request->all();

            $params = json_decode($params['params'], true);

            $where = $params['search'] ?? '';
            $limit = $params['pageSize'] ?? '';
            $offset = isset($params['pageCode']) ? ($params['pageCode'] - 1) * $limit : '0';

            $query = Permission::query();

            //获取数据
            $data = $query->where($where)
                ->limit($limit)
                ->offset($offset)
                ->get();

            //获取数据总数
            $total = $query->count();

            //获取分组类别
            $group = $query->select('group_name as text', 'group_name as value')
                ->where($where)
                ->limit($limit)
                ->offset($offset)
                ->groupBy('group_name')
                ->get();

            return $this->successReturn(200, [
                'total' => $total,
                'list' => [
                    'list' => $data,
                    'group' => $group
                ]
            ]);

        } catch (\Exception $e) {
            return $this->errorReturn(500, $e->getMessage());
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            $params = [
                'roleId' => $request->roleId,
                'permissionsAllow' => $request->permissionsAllow
            ];
            //配置验证
            $rules = [
                'roleId' => 'required',
                'permissionsAllow' => 'required',
            ];
            $message = [
                'roleId.required' => '[roleId]缺失',
                'permissionsAllow.required' => '请至少选择一个权限',
            ];

            $this->verifyParams($params, $rules, $message);

            $role = Role::findById($params['roleId']);

            DB::beginTransaction();

            //先清空当前用户所有权限
            DB::table('role_has_permissions')
                ->where('role_id', $params['roleId'])
                ->delete();

            foreach ($params['permissionsAllow'] as $key) {
                if (!$role->givePermissionTo($key)) $this->throwExp('添加角色权限失败');
            }

            DB::commit();

            return $this->successReturn(200, '添加角色权限成功');

        } catch (\Exception $e) {
            return $this->errorReturn(500, $e->getMessage());
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
